<?php


namespace app\sports\analysis\service;


/*
 * 足球分析数据
 */

use think\Db;
use think\Exception;
use app\common\HTTP;
use app\common\OSS;
use think\facade\Log;
use app\sports\analysis\model\AnalysisModel;
use app\common\InstanceTrait;

class AnalysisCaiji
{
    private $user = '';
    private  $secret ='';
    private  $url = 'open.sportnanoapi.com/api/sports/football/match/analysis';
//    接口名:足球分析数据
//    接口频次:1/s  建议5分钟一次

    public function  __construct()
    {
        $this->user = Env::get('nami.user');
        $this->secret = Env::get('nami.secret');
    }

    /*
     * @$id 纳米id
     *return int 受到影响行数
     */

    public function Execs($id=21155)
    {
        try{
//        远程获取数据
          $analyslsressult = json_decode(HTTP::getInstance()->doGet($this->url."?user=$this->user&secret=$this->secret&id=$id"), true);

//        思路:
//        先查询数据库是否存在
          $resultdata=AnalysisModel::getInstance()->IsAnalysis();
          $data=[
                  'nami_match_id'    =>$id,
                  'injury'           =>json_encode($analyslsressult['injury'],JSON_UNESCAPED_UNICODE),
                  'integral'         =>json_encode($analyslsressult['integral'],JSON_UNESCAPED_UNICODE),
                  'history'          =>json_encode($analyslsressult['history'],JSON_UNESCAPED_UNICODE),  //
                  'left_history'     =>json_encode(),  //这个字段需要具体查询接口返回数据
                  'right_history'    =>json_encode(),  //这个字段需要具体查询接口返回的数据
                  'create_time'      =>date('Y-h-d H:i:s',time()),
                  'update_time'      =>date('Y-h-d H:i:s',time()),
              ];
          if (!$resultdata){
//              添加数据
               $result=AnalysisModel::getInstance()->InsertAnalysis($data);
          }else{
//              更新数据
               $result=AnalysisModel::getInstance()->UpdateAnalysis($data);
          }
            if (!$result) throw new Exception('足球分析数据入库失败');

            Log::log('info','足球分析采集成功');
        }catch (Exception $e){
            Log::error('足球分析数据错误:'.$e->getMessage());
            return [];
        }
    }
}